//快手直投 详情文档https://www.yuque.com/u37167570/re8qvt/dtvgcxx3lsi5kgz6/edit

export default class ks_analytics {
    /**
     * 初始化(登录code)
     * @param {*} appid 
     * @param {*} jscode 
     * @param {*} query 
     * @param {*} success 
     * @param {*} fail 
     * @returns 
     */
    public static init(appid = "", jscode = "", query = {}, success = (param?) => { }, fail = (param?) => { }) {
        if (jscode == "") {
            console.error("参数code错误")
            return
        }

        let param = {
            app_id: appid,
            js_code: jscode,
            query: JSON.stringify(query)
        }
        console.log("param", param)

        ks_analytics.httpPost("init_code", param, (arg) => {
            console.log(arg)
            if (arg.result == null) {
                fail(arg)
                return
            }
            success(arg)
        }, "https://kszt-sdk.tianjinyuren.cn/ks");
    }

    /**
     * 初始化(openid)
     * @param {*} appid 
     * @param {*} openid 
     * @param {*} unionid 
     * @param {*} query 
     * @param {*} success 
     * @param {*} fail 
     * @returns 
     */
    public static init_uid(appid = "", openid = "", unionid = "test_union_id", query = {}, success = (param?) => { }, fail = (param?) => { }) {

        let param: any = {
            app_id: appid,
            open_id: openid,
            union_id: unionid,
            query: JSON.stringify(query)
        }
        console.log("param", param)

        ks_analytics.httpPost("init_uid", param, (arg) => {
            console.log(arg)
            if (arg.code != 0) {
                fail(arg)
                return
            }
            success(arg)
        }, "https://kszt-sdk.tianjinyuren.cn/ks");
    }

    /**
     * 回传看完视频
     * @param {*} openid 
     * @param {*} success 
     * @param {*} fail 
     */
    public static ad_complete(openid = "", success = (param?) => { }, fail = (param?) => { }) {
        let param = {
            open_id: openid,
        }
        //console.log("param", param)

        ks_analytics.httpPost("handle_ad_complete", param, (arg) => {
            console.log(arg)
            if (arg.code != 0) {
                fail(arg)
                return
            }
            success(arg)
        }, "https://kszt-sdk.tianjinyuren.cn/ks");
    }

    /**
     * 回传曝光视频
     * @param {*} openid 
     * @param {*} success 
     * @param {*} fail 
     */
    public static ad_exposure(openid = "", success = (param?) => { }, fail = (param?) => { }) {
        let param = {
            open_id: openid,
        }

        ks_analytics.httpPost("handle_ad_exposure", param, (arg) => {
            console.log(arg)
            if (arg.code != 0) {
                fail(arg)
                return
            }
            success(arg)
        }, "https://kszt-sdk.tianjinyuren.cn/ks");
    }


    public static httpGet(path, data, handler, extraUrl) {
        var xhr = new XMLHttpRequest();
        var requestUrl = extraUrl + "/" + path;
        xhr.open("GET", requestUrl, true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    var ret = JSON.parse(xhr.responseText);
                    if (handler !== null) {
                        handler(ret);
                    }
                } else {
                    console.error("httpGet status -->", xhr.status);
                }
            }
        }
        xhr.send(data);
    }

    public static httpPost(path, data, handler, extraUrl) {
        var xhr = new XMLHttpRequest();
        var requestUrl = extraUrl + "/" + path;
        xhr.open("POST", requestUrl, true);
        xhr.setRequestHeader("Content-type", 'multipart/form-data');
        xhr.onreadystatechange = function () {//请求后的回调接口，可将请求成功后要执行的程序写在其中
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    var ret = JSON.parse(xhr.responseText);
                    if (handler !== null) {
                        handler(ret);
                    }
                } else {
                    console.error("httpGet status -->", xhr.status);
                }
            }
        };
        xhr.send(data);
    }
}